package com.easy.common.constant;
import java.io.File;
import java.util.*;

import com.easy.common.util.Utility;

public class SysProps {
	private static String adminName = null;
	private static Properties props = null;
	private static String osName = System.getProperty("os.name");

	public SysProps()
	{
	}

	public static String getProp(String key)
	{
		if (props == null)
			return "";
		String prop = props.getProperty(key);
		if (prop != null)
			prop = prop.trim();
		else
			prop = "";
		return prop;
	}

	public static String getString(String key)
	{
		return getProp(key);
	}

	public static int getInt(String key)
	{
		int rtInt = 0;
		try
		{
			String strValue = getString(key);
			if (!Utility.isNullorEmpty(strValue))
				rtInt = Integer.parseInt(strValue);
		}
		catch (Exception exception) { }
		return rtInt;
	}

	public static long getLong(String key)
	{
		long rtLong = 0L;
		try
		{
			String strValue = getString(key);
			if (!Utility.isNullorEmpty(strValue))
				rtLong = Long.parseLong(strValue);
		}
		catch (Exception exception) { }
		return rtLong;
	}

	public static void setProps(Properties props1)
	{
		props = props1;
	}

	public static void updateProp(Object key, Object value)
	{
		props.put(key, value);
	}

	public static void addProps(Map aProps)
	{
		if (aProps == null)
			return;
		String key;
		String value;
		Iterator iKeys = aProps.keySet().iterator();
		while (iKeys.hasNext()) {
			key = (String)iKeys.next();
			value = (String)aProps.get(key);
			props.put(key, value);
		}
	}

	public static String getIdName()
	{
		String fieldName = getString("db.idfield.name");
		if (Utility.isNullorEmpty(fieldName))
			return "SEQ_ID";
		else
			return fieldName;
	}

	public static String getRootPath()
	{
		return getString("rootDir");
	}

	public static String getWebPath()
	{
		return (new StringBuilder(String.valueOf(getString("rootDir")))).append(File.separator).append(getString("webRootDir")).append(File.separator).append(getString("jspRootDir")).toString();
	}

	public static String getWebInfPath()
	{
		return (new StringBuilder(String.valueOf(getString("rootDir")))).append(File.separator).append(getString("webRootDir")).append(File.separator).append(getString("jspRootDir")).append(File.separator).append("WEB-INF").toString();
	}

	public static String getAdminName()
	{
		if (adminName != null)
			return adminName;
		adminName = getString("sysAdminName");
		if (Utility.isNullorEmpty(adminName))
			adminName = "admin";
		return adminName;
	}

	public static String getWebInfPathRela()
	{
		return (new StringBuilder(String.valueOf(getString("webRootDir")))).append(File.separator).append(getString("jspRootDir")).append(File.separator).append("WEB-INF").toString();
	}

	public static String getStylePath()
	{
		return (new StringBuilder(String.valueOf(getString("rootDir")))).append(File.separator).append(getString("webRootDir")).append(File.separator).append(getString("jspRootDir")).append(File.separator).append("easy").append(File.separator).append("styles").toString();
	}

	public static String getUiPath()
	{
		return (new StringBuilder(String.valueOf(getString("rootDir")))).append(File.separator).append(getString("webRootDir")).append(File.separator).append(getString("jspRootDir")).append(File.separator).append("easy").append(File.separator).append("ui").toString();
	}

	public static String getUiTemplatePath()
	{
		return (new StringBuilder(String.valueOf(getString("rootDir")))).append(File.separator).append(getString("webRootDir")).append(File.separator).append(getString("jspRootDir")).append(File.separator).append("easy").append(File.separator).append("templates").toString();
	}

	public static String getLogConfigPath()
	{
		return (new StringBuilder(String.valueOf(getWebInfPath()))).append(File.separator).append("classes").append(File.separator).append("log4j.properties").toString();
	}

	public static String getSysConfigPath()
	{
		return (new StringBuilder(String.valueOf(getWebInfPath()))).append(File.separator).append("config").append(File.separator).append("sysconfig.properties").toString();
	}

	public static String getSysDbName()
	{
		String sysDbName = getString("sysDatabaseName");
		if (sysDbName == null)
			sysDbName = "MY";
		return sysDbName;
	}

	public static String getSysDbDsName()
	{
		String sysDbDsName = getString("db.jdbc.datasource.sysDsName");
		if (sysDbDsName == null)
			sysDbDsName = "mssql/my";
		return sysDbDsName;
	}

	public static String getUploadCatchPath()
	{
		String cachPath = getString("fileUploadTempDir");
		if (cachPath == null)
			cachPath = (new StringBuilder(String.valueOf(getString("rootDir")))).append(File.separator).append("catch").toString();
		if (cachPath.indexOf(":") == 1 || cachPath.indexOf("/") == 0)
		{
			return cachPath;
		} else
		{
			cachPath = getString((new StringBuilder("rootDir")).append(File.separator).append(cachPath).toString());
			return cachPath;
		}
	}

	public static String getAttachPath()
	{
		String attachPath = getString("attachFilePath");
		if (attachPath == null)
			attachPath = (new StringBuilder(String.valueOf(getString("rootDir")))).append(File.separator).append("attach").toString();
		if (attachPath.indexOf(":") == 1 || attachPath.indexOf("/") == 0)
		{
			return attachPath;
		} else
		{
			attachPath = (new StringBuilder(String.valueOf(getString("rootDir")))).append(File.separator).append(attachPath).toString();
			return attachPath;
		}
	}

	public static String getLimitUploadFiles()
	{
		String allowUploadFiles = getString("limitUploadFiles");
		if (allowUploadFiles == null)
			allowUploadFiles = "";
		return allowUploadFiles;
	}

	public static Map startsWith(String prefix)
	{
		return Utility.startsWithMap(props, prefix);
	}

	public static Map endsWith(String postFix)
	{
		return Utility.endsWithMap(props, postFix);
	}

	public static boolean isWindows()
	{
		return Utility.null2Empty(osName).toLowerCase().indexOf("windows") >= 0;
	}

	public static boolean isLinux()
	{
		return Utility.null2Empty(osName).toLowerCase().indexOf("linux") >= 0;
	}
	public static boolean isMac()
	{
		return Utility.null2Empty(osName).toLowerCase().indexOf("Mac") >= 0;
	}
}
